﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using BUS;
using System.Security.Cryptography;

namespace BUS
{
    public class NguoiDungBus
    {
        public static String EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }

        public static bool ThemNguoiDung(NguoiDungDto one)
        {
            bool result = false;
            one.MatKhau = EncodePassword(one.MatKhau);
            if (NguoiDungDao.KiemtraTontai(one) == false)
                result = NguoiDungDao.ThemNguoiDung(one);

            return result;
        }

        public static bool ThaydoiMatKhauNguoiDung(NguoiDungDto one, String matkhau)
        {
            bool result = false;
            matkhau = EncodePassword(matkhau);
            if (NguoiDungDao.KiemtraTontai(one) == true)
                result = NguoiDungDao.ThaydoiMatKhauNguoiDung(one, matkhau);

            return result;
        }

        public static bool PhanquyenNguoiDung(NguoiDungDto one, int loainguoidung)
        {
            bool result = false;
            if (NguoiDungDao.KiemtraTontai(one) == true)
                result = NguoiDungDao.PhanquyenNguoiDung(one, loainguoidung);

            return result;
        }

        public static bool CapnhatTrangThaiNguoiDung(NguoiDungDto one, String trangthai)
        {
            bool result = false;
            if (NguoiDungDao.KiemtraTontai(one) == true)
                result = NguoiDungDao.CapnhatTrangThaiNguoiDung(one, trangthai);

            return result;
        }
        public static bool XoaNguoiDung(NguoiDungDto one)
        {
            bool result = false;
            if (NguoiDungDao.KiemtraTontai(one) == true)
                result = NguoiDungDao.XoaNguoiDung(one);

            return result;
        }

        public static bool CapnhatNguoiDung(NguoiDungDto one)
        {
            bool result = false;
            if (NguoiDungDao.KiemtraTontai(one) == true)
                result = NguoiDungDao.CapnhatNguoiDung(one);

            return result;
        }

        public static NguoiDungDto LayNguoiDungTheoID(int ID)
        {
            return NguoiDungDao.LayNguoiDungTheoID(ID);
        }

        public static NguoiDungDto LayNguoiDungTheoTen(String ten)
        {
            return NguoiDungDao.LayNguoiDungTheoTen(ten);
        }


        public static List<NguoiDungDto> LayNguoiDungTheoLoai(int loainguoidung)
        {
            return NguoiDungDao.LayNguoiDungTheoLoai(loainguoidung);
        }

        public static List<NguoiDungDto> LayNguoiDungTimKiem(String keyword)
        {
            return NguoiDungDao.LayNguoiDungTimKiem(keyword);
        }
        public static List<NguoiDungDto> LayKhachHangTimKiem(String keyword)
        {
            return NguoiDungDao.LayKhachHangTimKiem(keyword);
        }
        public static List<NguoiDungDto> LayTatCaNguoiDung()
        {
            return NguoiDungDao.LayTatCaNguoiDung();
        }
        public static List<NguoiDungDto> LayTatCaKhachHang()
        {
            return NguoiDungDao.LayTatCaKhachHang();
        }
        public static List<String> LayNguoiDungTrangThai()
        {
            return NguoiDungDao.LayTrangThaiNguoiDung();
        }
        private static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private static string RandomString(int size, bool lowerCase)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        public static string GeneratePassword()
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append(RandomString(4, true));
            builder.Append(RandomNumber(1000, 9999));
            builder.Append(RandomString(2, false));
            return builder.ToString();
        }

        //<NEW CODE>
        public static bool CongDiemNguoiDung(int id, int Diem)
        {
            return NguoiDungDao.CongDiemNguoiDung(id, Diem);
        }
        //</NEW CODE>
    }
}
